/******************************************************************************

  Copyright (C), 2001-2011, DCN Co., Ltd.

 ******************************************************************************
  File Name     : Board.h
  Version       : Initial Draft
  Author        : Hust
  Created       : 2022/11/5
  Last Modified :
  Description   : can_boot.c header file
  Function List :
  History       :
  1.Date        : 2022/11/5
    Author      : Hust
    Modification: Created file

******************************************************************************/
#ifndef __BOARD_H__
#define __BOARD_H__

/*----------------------------------------------*
 * include headfile                             *
 *----------------------------------------------*/
#include "stm32f1xx_hal.h"
#include "SEMTypes.h"
#include "System1Data.h"
#include "Def_Type.h"
#include "flash_if.h"
#include "string.h"
#include "stdio.h"
#include "SEGGER_RTT.h"
#include "SEMBDef.h"

/*----------------------------------------------*
 * macros                                       *
 *----------------------------------------------*/
#define DEBUG_VER

#ifdef DEBUG_VER
#define CONSOLE_MSG 1
//#define MSG_DBG(flag,Str)  (void)((flag)? printf Str,1:0)

#define RTT_MSG(...) SEGGER_RTT_printf(0,__VA_ARGS__)
#define MSG_DBG(flag,Str)  (void)((flag)? RTT_MSG Str,1:0)

#define DebugTrace(...)   SEGGER_RTT_printf(0,__VA_ARGS__)

#else
#define CONSOLE_MSG 0
#define MSG_PRINT(flag,Str)
#define DebugTrace

#endif
#define GPIO_SN_PORT GPIOA
#define GPIO_SN0 GPIO_PIN_0
#define GPIO_SN1 GPIO_PIN_1
#define MODULE_SN HAL_GPIO_ReadPin(GPIO_SN_PORT, GPIO_PIN_0)


/* CAN ID assingment */
#define CAN_CMD_ID_TPYE  CAN_ID_STD
#define CAN_CMD_FRAME_ID 	(uint32_t)(0x0E0U)
/* A packet  consist of 1 to 16 Frames  */
//#define CAN_PACK_FRAME_RETRY_ID (uint32_t)(0x1FFFFEEU)
#define CAN_RESP_FRAME_ID 	(CAN_CMD_FRAME_ID+8)

//#define CAN_PACK_FRAME_END_ID 	(uint32_t)(0x1FFFFEFU)
#define CAN_PACK_FRAME0_ID  (CAN_CMD_FRAME_ID+16)      
#define PACK_FRAME_MAX 16
#define CAN_PACK_FRAME_END_ID 	(CAN_PACK_FRAME0_ID+PACK_FRAME_MAX)

#define PACK_SIZE_MAX (PACK_FRAME_MAX*8)
#define PACK_ERR_TABLE_SIZE 16

/* define timer act and timer stop act */
#if 0
#define TIMER_VAR_DEF(timer)  FlagStatus g##timer##_start_flag = RESET;\
							VS_UINT 	g##timer##_cnt = 0;\
							SEM_EVENT_TYPE  	g##timer##_call_event = Null_Ev

#define TIMER_VAR_DECLARE(timer)  extern FlagStatus g##timer##_start_flag ;\
							extern VS_UINT 	g##timer##_cnt ;\
							extern SEM_EVENT_TYPE  	g##timer##_call_event 


#define TIMER_ACT(timer,ev,tick) 	g##timer##_start_flag =SET;\
									g##timer##_cnt=tick;\
									g##timer##_call_event = ev;

#define TIMER_STOP_ACT(timer) 	g##timer##_start_flag =RESET;\
									g##timer##_cnt=0;\
									g##timer##_call_event = Null_Ev;
#define TIMER_CNT_SUB_ROUTINE(timer) do{\
										if (g##timer##_start_flag != RESET)\
										{\
											if (g##timer##_cnt != 0)\
												g##timer##_cnt--;\
											else \
											{\
												g##timer##_start_flag = RESET;\
												SEQ_AddEvent((SEM_EVENT_TYPE) g##timer##_call_event);\
											}\
											}\
										}	while(0)
#else
#define  TIMER_ACT(timer,ms,ev) timer.start_flag = SET;\
								timer.cnt = ms;\
								timer.call_ev = ev;

#define TIMER_STOP_ACT(timer) timer.start_flag = RESET;\
								timer.cnt = 0;

#define TIMER_CNT_SUB_BLOCK(timer) do{\
										if (timer.start_flag != RESET)\
										{\
											if (timer.cnt != 0)\
												timer.cnt--;\
											else \
											{\
												timer.start_flag = RESET;\
												SEQ_AddEvent((SEM_EVENT_TYPE) timer.call_ev);\
											}\
											}\
										}	while(0)



#endif


/*----------------------------------------------*
 * variables typedef                             *
 *----------------------------------------------*/
typedef struct TIMER_MS
{
	uint8_t start_flag;
    uint16_t cnt ;
	SEM_EVENT_TYPE call_ev;
}TIMER_MS_STRU;

typedef struct CAN_OTA_INFO
{
	uint32_t ota_module_id;
	uint32_t ota_module_mask;
	uint32_t ota_hw_ver;
	uint32_t ota_sw_ver;
	uint32_t erase_addr;
	uint32_t erase_length;
	uint32_t bin_flash_start_addr;
	uint32_t *bin_flash_ptr;
	uint32_t bin_size;
	uint32_t bin_crc;
	uint32_t bin_sent_cnt;
	uint32_t bin_rcved_cnt;
	uint32_t bin_rcved_crc;
	uint32_t pack_pre_index;
	uint32_t pack_cur_index;
	uint16_t pack_size;
	uint16_t pack_rcved_cnt;
	uint16_t pack_sent_cnt;
	uint32_t pack_crc;
	uint8_t cmd;
    uint16_t timeout1; 
	uint16_t retry_time; 
	uint8_t buf[8];
	uint8_t pack[PACK_SIZE_MAX];
	uint8_t *pack_ptr;
	uint32_t pack_err_table[PACK_ERR_TABLE_SIZE];
	uint8_t err_idx;
//	uint16_t time
}CAN_OTA_INFO_STRU;

typedef struct CAN_RX_MSG
{
    CAN_RxHeaderTypeDef header  ;
	uint8_t 			data[8];
}CAN_RX_MSG_STRU;

typedef struct CAN_TX_MSG
{
    CAN_TxHeaderTypeDef header  ;
	uint8_t 			data[8];
}CAN_TX_MSG_STRU;


typedef enum MSG_TYPE
{
NULL_frame = (uint8_t) 0, 
INVALID_frame, 
ERROR_frame, 

/* cmd frame*/
VALID_CMD_start = (uint8_t) 0x10, 
RESTART_cmd = (uint8_t) 0x11, 
JUMP_APP_cmd = (uint8_t) 0x12, 
BUS_SILENCE_cmd = (uint8_t) 0x13, 
BUS_RESTORE_cmd = (uint8_t) 0x14, 
SYS_POLL_MODL_cmd = (uint8_t) 0x1E, 
MODULE_OTA_SHORTCUT_cmd = (uint8_t) 0x1F, 
OTA_START_cmd = (uint8_t) 0x20, 
OTA_TERMINAL_cmd = (uint8_t) 0x21, 
OTA_VER_cmd = (uint8_t) 0x22, 
OTA_ERASE_cmd = (uint8_t) 0x23, 
OTA_PARA_cmd = (uint8_t) 0x24, 
BIN_TRANS_START_cmd = (uint8_t) 0x2A, 
BIN_CPLT_cmd = (uint8_t) 0x2B, 
PACK_BEGIN_cmd = (uint8_t) 0x30, 
PACK_FINISH_cmd = (uint8_t) 0x31, 
BIN_FRAME_cmd = (uint8_t) 0x3A, 
VALID_CMD_end , 

/* response */
VALID_RESP_start = (uint8_t) 0x40, 
TIME_OUT_resp, 
ERASE_OTA_resp, 
RETRANS_REQ_req, 
OTA_FAIL_resp, 
OTA_SUCCESS_resp, 
BIN_FRAME_MISS_resp, 
VALID_RESP_end, 

/* bin frame */
BIN_frame_start = (uint8_t) 0x80, 
BIN_frame_end = (uint8_t) 0xC0, 
} MSG_TYPE_ENUM;


typedef enum MODULE_ST
{
	MODULE_BUS_SILENCE_st = (uint8_t)1,
	MODULE_BUS_NORMAL_st = (uint8_t)2,

                 
}MODULE_ST_ENUM;

typedef struct MODL_INFO
{
    uint32_t dev_hw_ver;
    uint32_t dev_sw_ver;	
	uint32_t dev_modl_type;
	uint32_t dev_modl_id;
	uint32_t dev_sn;
	uint8_t  module_silence_st;
	
}MODL_INFO_STRU;

typedef enum PACK_INDEX_ST
{
	PK_IDX_INVALID = (uint8_t) 0, 
	PK_IDX_MISS_OVERFLOW = (uint8_t) 1, 
	PK_IDX_MISS, 
	PK_IDX_CONTIONUS, 
	PACK_IN_ERROR_TABLE, 
	PACK_NOT_IN_ERROR_TABLE = (uint8_t) 0xff
} PACK_INDEX_ST_ENUM;



typedef enum CAN_MSG_ID
{
/* 0x1FFFFFFC0-0x1FFFFFFF， */
OTA_CMD_ID = (uint32_t) 0x1FFFFFFC0U, 
OTA_RESP_ID = (uint32_t) 0x1FFFFFFC1U, 

//	BinFrame_start= (uint32) 0x1FFFFFFCF,
BinFrame0_ID = (uint32_t) 0x1FFFFFFD0U, 
BinFrame1_ID, 
BinFrame2_ID, 
BinFrame3_ID, 
BinFrame4_ID, 
BinFrame5_ID, 
BinFrame6_ID, 
BinFrame7_ID, 
BinFrame8_ID, 
BinFrame9_ID, 
BinFrame10_ID, 
BinFrame11_ID, 
BinFrame12_ID, 
BinFrame13_ID, 
BinFrame14_ID, 
BinFrame15_ID, 
BinFrameEND_ID										//	BinPack_end= (uint32) 0x1FFFFF0,

//	BinCplt
} CAN_MSG_ID_ENUM;

//FLASH_WRITE_ENUM gTrans_state = BIN_TRANS;
/*----------------------------------------------*
 * constants                                    *
 *----------------------------------------------*/

/*----------------------------------------------*
 * external variables                           *
 *----------------------------------------------*/
extern CAN_RX_MSG_STRU gRxMsg;
extern CAN_TX_MSG_STRU gTxMsg;

extern CAN_OTA_INFO_STRU gOta_info;

extern MODL_INFO_STRU gModlInfo ;

extern TIMER_MS_STRU gTimer0;
extern TIMER_MS_STRU gTimer1;

extern __no_init  uint32_t mOtaFlag @ APP_UPDATE_KEY_ADDRESS;

/*----------------------------------------------*
 * project-wide global variables                *
 *----------------------------------------------*/
//TIMER_VAR_DECLARE(Timer0);
//TIMER_VAR_DECLARE(Timer1);

extern MODL_INFO_STRU gModlInfo ;

/*----------------------------------------------*
 * module-wide global variables                 *
 *----------------------------------------------*/

/*----------------------------------------------*
 * external routine prototypes                  *
 *----------------------------------------------*/
extern void JumpToApplication(uint32_t Addr);
extern void CAN_Config(CAN_HandleTypeDef * hcan);
extern void TimerCnt(void);
extern void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan);
extern void HAL_CAN_RxFifo0FullCallback(CAN_HandleTypeDef *hcan);

/*----------------------------------------------*
 * internal routine prototypes                  *
 *----------------------------------------------*/

/*----------------------------------------------*
 * routines' implementations                    *
 *----------------------------------------------*/


#ifdef __cplusplus
#if __cplusplus
extern "C"{
#endif
#endif /* __cplusplus */


#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */


#endif /* __BOARD_H__ */
